<%--
    Document   : doSearch
    Author     : Mike
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="searchInput" class="bookDatabase_pkg.BookInfo" scope="session"/>
<jsp:setProperty name="searchInput" property="*"/>

<%@ include file="header.jsp" %>

<!-- Content -->

<div id="container">
    <div id="content" role="main">
        <div id="post-0" class="post error404 not-found">
            <h1 class="entry-title">Search Results</h1>
            <div class="entry-content">
                <%
                if(searchInput.getBookTitle() != null){
                    String tempBookQuery = searchInput.getBookTitle();
                    out.println("<p>Click on a book to view details:</p>");

                    out.println("<h2>Action</h2><p></p>");

                    bookDatabase_pkg.bookDatabaseDriver driverSearch = new bookDatabase_pkg.bookDatabaseDriver();
                    String categorySearch = "action";
                    int resultSearch = driverSearch.databaseConnect(categorySearch);
                    if(resultSearch == 0){
                        //out.println("Database: " + categorySearch + " connected<br />");

                        String[][] booksSearch = driverSearch.getAllBooks();
                        out.println("<ul>");

                            int bookCount = 0;

                            for(int i=0; i<booksSearch.length; i++){
                                String titleLower = booksSearch[i][1].toLowerCase();
                                String queryLower = tempBookQuery.toLowerCase();

                                //out.println("titleLower: " + titleLower);
                                //out.println("queryLower: " + queryLower);
                                //if(titleLower.contains(queryLower)){
                                //    out.println("titleLower contains queryLower");
                                //}

                                if(titleLower.contains(queryLower)){
                                    bookCount++;

                                    out.println("<form name='bk" + booksSearch[i][0] + "' role='viewBook' method='get' id='searchform' action='book.jsp'>");
                                    out.println("<input type='hidden' value=" + categorySearch + " name='category' id='category' />");
                                    out.println("<input type='hidden' value=" + booksSearch[i][0] + " name='ISBN' id='ISBN' />");
                                    out.println("<a href='' onclick='document.bk" + booksSearch[i][0] + ".submit(); return false;'><li>" + booksSearch[i][1] + "</li></a>");
                                    out.println("</form>");

                                }
                            }

                            if(bookCount == 0){
                                out.println("No Book Titles in this category contain '" + tempBookQuery + "'");
                                bookCount = 0;
                            }
                        
                        out.println("</ul>");

                    }
                    else{
                        out.println("ERROR: Database " + categorySearch + " cannot be found!<br />");
                    }

                    out.println("<h2>Drama</h2><p></p>");

                    driverSearch = new bookDatabase_pkg.bookDatabaseDriver();
                    categorySearch = "drama";
                    resultSearch = driverSearch.databaseConnect(categorySearch);
                    if(resultSearch == 0){
                        //out.println("Database: " + categorySearch + " connected<br />");

                        String[][] booksSearch = driverSearch.getAllBooks();
                        out.println("<ul>");

                            int bookCount = 0;

                            for(int i=0; i<booksSearch.length; i++){
                                String titleLower = booksSearch[i][1].toLowerCase();
                                String queryLower = tempBookQuery.toLowerCase();

                                //out.println("titleLower: " + titleLower);
                                //out.println("queryLower: " + queryLower);
                                //if(titleLower.contains(queryLower)){
                                //    out.println("titleLower contains queryLower");
                                //}

                                if(titleLower.contains(queryLower)){
                                    bookCount++;

                                    out.println("<form name='bk" + booksSearch[i][0] + "' role='viewBook' method='get' id='searchform' action='book.jsp'>");
                                    out.println("<input type='hidden' value=" + categorySearch + " name='category' id='category' />");
                                    out.println("<input type='hidden' value=" + booksSearch[i][0] + " name='ISBN' id='ISBN' />");
                                    out.println("<a href='' onclick='document.bk" + booksSearch[i][0] + ".submit(); return false;'><li>" + booksSearch[i][1] + "</li></a>");
                                    out.println("</form>");

                                }
                            }

                            if(bookCount == 0){
                                out.println("No Book Titles in this category contain '" + tempBookQuery + "'");
                                bookCount = 0;
                            }

                        out.println("</ul>");

                    }
                    else{
                        out.println("ERROR: Database " + categorySearch + " cannot be found!<br />");
                    }

                    out.println("<h2>Comedy</h2><p></p>");

                    driverSearch = new bookDatabase_pkg.bookDatabaseDriver();
                    categorySearch = "comedy";
                    resultSearch = driverSearch.databaseConnect(categorySearch);
                    if(resultSearch == 0){
                        //out.println("Database: " + categorySearch + " connected<br />");

                        String[][] booksSearch = driverSearch.getAllBooks();
                        out.println("<ul>");

                            int bookCount = 0;

                            for(int i=0; i<booksSearch.length; i++){
                                String titleLower = booksSearch[i][1].toLowerCase();
                                String queryLower = tempBookQuery.toLowerCase();

                                //out.println("titleLower: " + titleLower);
                                //out.println("queryLower: " + queryLower);
                                //if(titleLower.contains(queryLower)){
                                //    out.println("titleLower contains queryLower");
                                //}

                                if(titleLower.contains(queryLower)){
                                    bookCount++;

                                    out.println("<form name='bk" + booksSearch[i][0] + "' role='viewBook' method='get' id='searchform' action='book.jsp'>");
                                    out.println("<input type='hidden' value=" + categorySearch + " name='category' id='category' />");
                                    out.println("<input type='hidden' value=" + booksSearch[i][0] + " name='ISBN' id='ISBN' />");
                                    out.println("<a href='' onclick='document.bk" + booksSearch[i][0] + ".submit(); return false;'><li>" + booksSearch[i][1] + "</li></a>");
                                    out.println("</form>");

                                }
                            }

                            if(bookCount == 0){
                                out.println("No Book Titles in this category contain '" + tempBookQuery + "'");
                                bookCount = 0;
                            }

                        out.println("</ul>");

                    }
                    else{
                        out.println("ERROR: Database " + categorySearch + " cannot be found!<br />");
                    }

                }
                else{
                    out.println("Search failed. Input can't be blank!");
                }

                searchInput.setAllNull();
            %>
            <hr/>
            </div><!-- .entry-content -->
	</div><!-- #post-0 -->
    </div><!-- #content -->
</div><!-- #container -->

<%@ include file="sidebar.jsp" %>

<!-- /Content -->

<%@ include file="footer.jsp" %>